package m.jclecai.com.fragment;

import android.text.TextUtils;
import android.widget.EditText;

import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import m.jclecai.com.R;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.MyToast;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;
import m.jclecai.com.utils.http.responseBody.BaseBody;

/**
 * Created by Dinosa on 2018/1/4.
 */

public class CovertRedFragment extends BaseFragment {


    @BindView(R.id.et_code)
    EditText etCode;

    @BindView(R.id.et_pwd)
    EditText etPwd;

    @Override
    public int getLayoutId() {
        return R.layout.activity_covert;
    }



    @OnClick(R.id.tv_convert)
    void convert() {
        if (TextUtils.isEmpty(etCode.getText().toString())) {
            MyToast.showToast(getActivity(), "请输入卡号");
            return;
        }
        if (TextUtils.isEmpty(etPwd.getText().toString())) {
            MyToast.showToast(getActivity(), "请输入卡密");
            return;
        }
        getRedPacket();
    }

    /**
     *
     */
    void getRedPacket() {
        showDialog();
        Map<String, String> pair = new HashMap<>();
        pair.put("user", PreferenceUtil.getUserName(getActivity()));//Yes	string	用户名
        pair.put("password", PreferenceUtil.getPassword(getActivity()));//Yes	string	密码
        pair.put("name", etCode.getText().toString());//Yes	卡号
        pair.put("newValue", etPwd.getText().toString());//Yes	卡密
        HttpManager.postAsyn(Constant_Url.USERREDCARD, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                dissDialog();
                DisPlay(getString(R.string.error_network));
            }

            @Override
            public void onResponse(String response) {
                dissDialog();
                BaseBody body = FastOk.get(response, BaseBody.class);
                if (body != null) {
                    DisPlay(body.getMessage());
                } else {
                    DisPlay(getString(R.string.error_network));
                }
            }
        }, pair);

    }
}
